package com.robot.admin.controller;

import com.robot.admin.pojo.City;
import com.robot.admin.pojo.Province;
import com.robot.admin.service.CityService;
import com.robot.admin.service.ProvinceService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * province控制器层
 *
 * @author lin
 */
@RestController
@CrossOrigin
@RequestMapping("/province")
public class ProvinceController {
    @Autowired
    private ProvinceService provinceService;
    @Autowired
    private CityService cityService;

    /**
     * 分页+多条件查询
     *
     * @param searchMap 查询条件封装
     * @param page      页码
     * @param size      页大小
     * @return 分页结果
     */
    @RequestMapping(value = "/findBySearch/{page}/{size}", method = RequestMethod.POST)
    public Result findBySearch(@RequestBody Map searchMap, @PathVariable int page, @PathVariable int size) {
        Page<Province> pageList = provinceService.findSearch(searchMap, page, size);
        return new Result(StatusCode.SUCCESS, new PageResult<Province>(pageList.getTotalElements(), pageList.getContent()));
    }

    /**
     * 根据code查找province
     *
     * @param province
     * @return
     */
    @RequestMapping(value = "findByCode", method = RequestMethod.POST)
    public Result findByCode(@RequestBody Province province) {
        return new Result(StatusCode.SUCCESS_QUERY, provinceService.findByCode(province.getCode()));
    }

    /**
     * 根据name查找province
     *
     * @param province
     * @return
     */
    @RequestMapping(value = "findByName", method = RequestMethod.POST)
    public Result findByName(@RequestBody Province province) {
        return new Result(StatusCode.SUCCESS_QUERY, provinceService.findByName(province.getName()));
    }


    /**
     * 根据code查找city列表
     *
     * @param province
     * @return
     */
    @RequestMapping(value = "findCitiesByCode/{page}/{size}", method = RequestMethod.POST)
    public Result findCitiesByCode(@RequestBody Province province, @PathVariable int page, @PathVariable int size) {
        Page<City> pageData = cityService.findAllByCodeLike(province.getCode(), page, size);
        return new Result(StatusCode.SUCCESS_QUERY, new PageResult<>(pageData.getTotalElements(), pageData.getContent()));
    }
}
